<template>
  <div class="login">
    <div class="box">
      <header><h2>登录</h2></header>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <div class="codeBox">
          <el-form-item label="验证码" prop="code">
            <el-input
              type="text"
              v-model="ruleForm.code"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <img @click="codeFun" :src="codeImg" alt="" />
        </div>
        <el-form-item>
          <el-button
            :class="isDisabled ? '' : 'successBtn'"
            :disabled="isDisabled"
            type="primary"
            @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { doLogin } from "@/api/request";
import { mapActions } from "vuex";
export default {
  data() {
    //密码
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        this.isBool.isPass = false;
        callback(new Error("请输入密码"));
      } else {
        if (/^\w{6,18}$/.test(value)) {
          callback();
          this.isBool.isPass = true;
        } else {
          this.isBool.isPass = false;
        }
      }
    };
    // 账号
    var validateusername = (rule, value, callback) => {
      if (value === "") {
        this.isBool.isName = false;
        callback(new Error("请输入账号"));
      } else {
        if (/^\w{5,18}$/.test(value)) {
          callback();
          this.isBool.isName = true;
        } else {
          this.isBool.isName = false;
        }
      }
    };
    var validatecode = (rule, value, callback) => {
      if (value === "") {
        this.isBool.isCode = false;
        callback(new Error("请输入验证码"));
      } else {
        this.isBool.isCode = true;
        callback();
      }
    };

    return {
      codeImg: "http://47.93.101.203/adminapi/captcha_pro?" + new Date(),
      isLogin: "",
      ruleForm: {
        pass: "",
        username: "",
        code: "",
      },
      isBool: {
        isName: false,
        isPass: false,
        isCode: false,
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "change" }],
        username: [{ validator: validateusername, trigger: "change" }],
        code: [{ validator: validatecode, trigger: "change" }],
      },
      fullPath: "",
    };
  },
  computed: {
    isDisabled() {
      if (this.isBool.isName && this.isBool.isPass && this.isBool.isCode) {
        return false;
      } else {
        return true;
      }
    },
    //  ...mapState("user",["userIfo"])
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.fullPath = from.path;
    });
  },
  methods: {
    ...mapActions("user", ["getUserIfosync"]),
    codeFun() {
      this.codeImg = "http://47.93.101.203/adminapi/captcha_pro?" + new Date();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //登陆请求验证
          clearTimeout(this.isLogin);
          this.isLogin = setTimeout(() => {
            doLogin({
              account: this.ruleForm.username,
              imgcode: this.ruleForm.code,
              pwd: this.ruleForm.pass,
            }).then((res) => {
              this.$message({
                message: res.status == 200 ? "登陆成功" : res.msg,
                type: res.status == 200 ? "success" : "error",
                duration: 1000,
              });
              console.log(res);
              if (res.status == 200) {
                this.getUserIfosync(res.data);
                sessionStorage.setItem("token", res.data.token);
                //判断当前路有对象是否是从别的路由跳转过来的
                if (this.fullPath == "/") {
                  this.$router.push("/");
                } else {
                  this.$router.back();
                }
              }
            });
          }, 500);
        } else {
          clearTimeout(this.isLogin);
          this.isLogin = setTimeout(() => {
            console.log("error submit!!");
          }, 500);
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="less" >
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.box {
  width: 500px;
  height: 500px;
  background: #ccc;
  margin-left: 15%;
  // margin-top: 35%;
  header {
    background: black;
    color: white;
    text-align: center;
  }
}
.codeBox {
  display: flex;
  width: 100%;
  img {
    width: 130px;
    height: 40px;
  }
  input {
    width: 270px;
  }
}
.el-form {
  padding: 10px;
}
.el-button {
  transition: all 0.3s ease-in;
}
.successBtn {
  width: 200px;
  background: green;
}
</style>
